import { goodList } from '@/api/goods'
import { IgoodList } from '@/types/goods'
import { defineStore } from 'pinia'

// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useGoodsStore = defineStore('goods', {
  state: () => {
    return {
      count:1
    }
  },
  actions: {
    resetCount(){
      this.count = 1
    }
  }
})

// 商品分类列表
export const useGoodListStore = defineStore('goodList', {
  state: () => {
    return {
      page:1,
      type:'',
      category_id:0,
      categories:[] as any,
      goods:[] as any
    }
  },
  actions: {
    // 获取侧边数据
    async getGoodList(){
      let list = {
        page:this.page,
        category_id:this.category_id
      } as IgoodList
      if(this.type === "sales"){
        list.sales = 1
      }else if(this.type === "price"){
        list.price = 1
      }
      let res = await goodList(list)
      if(res.status === 200){
        this.categories = res.data.categories
        this.goods = res.data.goods
      }
    },
    // 排序
    handleSort(type:any){
      if(type.name === "sales"){
        this.type = "sales"
      }else if(type.name === "price"){
        this.type = "price"
      }
      this.getGoodList()
    },
    // 分类
    handleCategory(id:number){
      this.category_id = id
      this.getGoodList()
    }
  }
})